#coding=UTF-8
'''
#十大股东
@author: joseph
'''
import datetime,os
from constant.Constant import *;
from xlrd import open_workbook
from xlutils.save import save
from xlwt.Workbook import Workbook
from xlwt import Style

#old='zcfzb';#TODO
report=u"最新截止日期",u"中间截止日期",u"最早截止日期"
url_sec='sdgd'#TODO
suffix='.html'
title_type='#EFEFEF'
data_type='#F6F6F6'
sheets={}
#stocks=[300153];# for test

url = concatUrl(url_head,concatUrl(url_sec, url_sec))

#return False if there are no data found        
def addRow(book,row=0,code=min_stock_code,type=data_type):
    u=url+str(code)+suffix;#add parameters in year
    soup = soupUrl(u);
    dataForm = soup.find('table',{'cellspacing':'2','cellpadding':'3'})
    col=0;
    for k in sheets.keys():
        if(data_type==type):
            sheets[k].write(row,col,code);                       
    if(dataForm==None):
        return False;
    datas=dataForm.findAll('td',{'bgcolor':type});#use find to get the first result TODO
    deadline=dataForm.findAll('td',{'rowspan':True});#use find to get the first result TODO
    if(title_type==type):#title
        for k in sheets.keys():
            sheets[k].write(row,col,'股票代码');
            col+=1
            sheets[k].write(row,col,'截止日期');
            col-=1;
        col+=1
        for n in range(0,11):#10shareholders and one sum
            for i in range(1,6):
                col+=1
                for k in sheets.keys():
                    sheets[k].write(row,col,datas[i].text);
    if(None==deadline):
        return True
    if(2==len(deadline)):
        deadline.insert(1,None)
    if(data_type==type):
        col+=1
        for date in deadline:
            column=col
            if(None!=date):
                i=deadline.index(date)
                key=report[i].encode(encode)
                sheets[key].write(row,column,date.text)#the deadline
                data=date.parent
                count=int(date['rowspan'])#sons of the tr
                #print(count)
                while count>1:
#                    print(date)
                    for d in data.findAll('td',{'bgcolor':type}):
                        column+=1
                        if('&nbsp;'!=d.text):
                            sheets[key].write(row,column,d.text)
#                        print(d)
                    count-=1
                    data=data.nextSibling.nextSibling
                column=10*5+2#sum
                for d in data.findAll('td',{'bgcolor':type}):
                    if('&nbsp;'!=d.text):
                        sheets[key].write(row,column,d.text) 
                    column+=1                     
    return True;

def addSheet(book):
    if(False==addRow(book,1,min_stock_code,title_type)):#add title
        return False;
    row=2;
    for code in stocks:
        print(code)
        addRow(book, row, code);#add data
        row=row+1;
    return True;
  
#十大股东
currentYear=datetime.date.today().year;
book=Workbook(encode);
sheets={}
for u in report:
    #year=y#the addRow use the year para
    name=u;
    print(name);
    newSheet=book.add_sheet(name);
    sheets[name.encode(encode)]=newSheet
    for c in range(1,11+1):
        #print(c)
        if(c!=11):
            newSheet.write(0,2+5*(c-1),c)
        else:
            newSheet.write(0,2+5*(c-1),'合计')
        newSheet.merge(0,0,2+5*(c-1),2+5*c-1)
addSheet(book)
book.save(u"十大股东.xls");
